package at.fhv.components.not;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import at.fhv.framework.CommonImageProcessingHelper;
import at.fhv.framework.events.ImageEvent;
import at.fhv.framework.listeners.ImageProcessingListener;
import at.fhv.framework.listeners.RefireListener;

/**
 * The NotFilter class.
 * 
 * @author Michael Sieber
 */
public class NotFilter implements ImageProcessingListener, RefireListener {
	private CommonImageProcessingHelper _helper;
	private PlanarImage _currImg;

	public NotFilter() {
		_helper = new CommonImageProcessingHelper(this);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void refire() {
		_helper.fireImageProcessingEvent(new ImageEvent(this, _currImg));
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void imageChanged(ImageEvent event) {
		// Set up the parameter block for the first op.
		ParameterBlock pb1 = new ParameterBlock();
		pb1.addSource(event.getImage());
		// Make first Op in Renderable chain an invert.
		_currImg = JAI.create("invert", pb1);
		_helper.fireImageProcessingEvent(new ImageEvent(this, _currImg));
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void addRefireListener(RefireListener listener) {
		_helper.addRefireListener(listener);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void removeRefireListener(RefireListener listener) {
		_helper.removeRefireListener(listener);
	}

	/**
	 * Add an image change listener
	 * 
	 * @param listener The image change listener to add
	 */
	public void addImageChangeListener(ImageProcessingListener listener) {
		_helper.addImageProcessingListener(listener);

		// force all previous beans to refire their event
		_helper.fireRefireEvent();
	}

	/**
	 * Remove an image change listener
	 * 
	 * @param listener The image change listener to remove
	 */
	public void removeImageChangeListener(ImageProcessingListener listener) {
		_helper.removeImageProcessingListener(listener);
	}

}
